<template>
  <div class="navigation">
    <ml-row>
      <ml-col :span='6'>
        <div class="clearfix">
          <a href="" class="navigator sidebar-toggler" @click.stop.prevent="toggleCollapse">
            <ml-icon name="chevron-right" style="font-size: 20px;line-height: inherit" v-if="collapse"></ml-icon>
            <ml-icon name="chevron-left" style="font-size: 20px;line-height: inherit" v-else></ml-icon>
          </a>
        </div>
      </ml-col>
      <ml-col :span="6">
        <div class="clearfix align-right">
          <div class="navigator cursor-default dropdown">
            <ml-popover thin minimal placement="bottom">
              <template slot="target">
                <div class="navigator dropdown-toggle" data-toggle="dropdown">
                  <ml-icon name="notifications"></ml-icon> 通知 <ml-icon name="chevron-down"></ml-icon>
                </div>
              </template>
              <div class="align-center no-notification">
                <div class="notification-icon">
                  <ml-icon name="notifications-updated"></ml-icon>
                </div>
                暂无消息
              </div>
            </ml-popover>
          </div>
          <div class="navigator cursor-default dropdown user-dropdown">
            <ml-popover thin minimal placement="bottom-end">
              <template slot="target">
                <div class="navigator dropdown-toggle user-avatar" data-toggle="dropdown">
                  <ml-avatar circle :size="32" intent="success" :text="profile.avatarText"></ml-avatar>
                  <div class="user-name">
                    {{profile.displayName}}
                    <ml-icon name="chevron-down"></ml-icon>
                  </div>
                </div>
              </template>
              <ml-menu no-shadow>
                <ml-menu-divider></ml-menu-divider>
                <ml-menu-item text="个人资料" icon="person"></ml-menu-item>
                <ml-menu-item text="偏好设置" icon="cog" label=""></ml-menu-item>
                <ml-menu-divider></ml-menu-divider>
                <ml-menu-item icon="log-out" @click="logout()" text="登出"></ml-menu-item>
              </ml-menu>
            </ml-popover>
          </div>
        </div>
      </ml-col>
    </ml-row>
  </div>
</template>

<script>
import store from '../utils/store'
export default {
  data () {
    var user = store.get('user')
    var username = (user || {}).username
    var userAvatarText = ''
    if (username && username[0]) {
      userAvatarText = username[0].toUpperCase()
    }
    var displayName = (user || {}).name || '用户详情'
    return {
      user: user,
      userAvatarImage: (user || {}).avatar,
      userAvatarText: userAvatarText,
      displayName: displayName
    }
  },
  computed: {
    profile () {
      var user = this.user
      if (user) {
        var displayName = user.name || user.username
        return {
          displayName: displayName,
          avatarImage: user.avatar,
          avatarText: displayName[0].toUpperCase()
        }
      } else {
        return {
          displayName: '用户详情',
          avatarImage: '',
          avatarText: ''
        }
      }
    }
  },
  mounted () {
    // this.$ws.on('notification', data => {
    //   console.log('Got Notification:', data)
    // })
    this.$event.on('user-updated', () => {
      this.user = store.get('user')
    })
  },
  methods: {
    toggleCollapse () {
      this.$emit('toggleCollapse')
    },
    logout () {
      this.$confirm({
        title: '提示',
        message: '是否确认登出？',
        confirm: {
          text: '登出',
          type: 'danger',
          icon: 'log-out'
        },
        cancel: {
          text: '取消'
        }
      }).then(() => {
        store.delete('user')
        store.delete('session')
        this.$event.emit('goto-logout')
        this.$message({
          type: 'success',
          message: '登出成功!'
        })
      }, () => {
        this.$message({
          type: 'info',
          message: '已取消登出'
        })
      })
    }
  },
  props: ['collapse']
}
</script>

<style lang="stylus" scoped>
@import "~@/stylesheets/vars"
.navigation {
  .navigator {
    &.user-avatar {
      padding: 10px 12px;
      .user-name {
        display: inline-block;
        vertical-align: top;
        line-height: 32px;
        padding-left: 8px;
      }
    }
  }
  .no-notification {
    padding: 20px;
    min-width: 160px;
    color: $gray;
    .notification-icon {
      margin: 0 auto 10px;
      font-size: 20px;
    }
  }
}
</style>
